export default ({
	namespaced: true,
	state() {
		return {
			// {goods_id,goods_name,goods_price,goods_count,goods_small_logo,goods_state}
			cart: JSON.parse(uni.getStorageSync('cart') || '[]')
		}
	},
	mutations: {
		addToCart(state, goods) {
			console.log(goods.goods_id);
			const findResult = state.cart.find(item => item.goods_id == goods.goods_id)
			console.log(findResult);
			if (!findResult) {
				state.cart.push(goods)
			} else {
				findResult.goods_count += 1
			}
			this.commit('m_cart/saveToStorage')
		},
		//修改物品的勾选状态
		changeGood_state(state, goods) {
			const findeResult = state.cart.find(item => item.goods_id == goods.goods_id)
			findeResult.goods_state = !findeResult.goods_state
			this.commit('m_cart/saveToStorage')
		},
		// 修改物品的数量
		changeGoods_count(state, goods) {
			const findResult = state.cart.find(item => item.goods_id == goods.goods_id)
			// console.log(findResult); 
			findResult.goods_count = goods.newValue
			this.commit('m_cart/saveToStorage')
		},
		// 删除商品
		delete_goods(state, goods) {
			const index = state.cart.findIndex(item => item.goods_id == goods.goods_id)
			state.cart.splice(index, 1)
			this.commit("m_cart/saveToStorage")
		},
		// 存储在本地
		saveToStorage(state) {
			uni.setStorageSync('cart', JSON.stringify(state.cart))
		},
		// 全选和全不选改变状态
		changeAll_GoodsState(state,ischange){
			state.cart.forEach(item=>{
				item.goods_state=ischange
			})
			this.commit("m_cart/saveToStorage")
		}
	},
	getters: {
		total(state) {
			let num = 0
			state.cart.forEach(item => {
				num += item.goods_count
			})
			return num
		},
		// 结算的数量
		getCheckNum(state) {
			let num = 0
			state.cart.forEach(item => {
				if (item.goods_state) {
					num += item.goods_count
				}
			})
			return num
		},
		//合计金额
		getCheckPrice(state) {
			let price = 0
			state.cart.forEach(item => {
				if (item.goods_state) {
					price += item.goods_price * item.goods_count
				}
			})
			return price
		},
	}

})
